package net.sansi.v3correctserver.config.matrixloader;

import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

/**
 * Created by Fang
 * create time : 2024/11/8
 * IDE : IntelliJ IDEA
 * <p>
 * double 数组文件加载器
 */
@Slf4j
public class DoubleArrMatrixFileLoader extends MatrixFileLoaderBase<double[]> {

    public DoubleArrMatrixFileLoader(int itemLen) {
        super(itemLen);
    }

    @Override
    int getItemSize(double[] item) {
        return item.length;
    }

    @Override
    int getItemBufferSize(double[] item) {
        return item.length * Double.BYTES;
    }

    @Override
    int getItemBufferSize(int len) {
        return len * Double.BYTES;
    }

    @Override
    void saveItemBuffer(ByteBuffer buffer, double[] item) throws Exception {
        for (double v : item) {
            buffer.putDouble(v);
        }
    }

    @Override
    double[] readItemBuffer(ByteBuffer buffer, int size) throws Exception {
        double[] res = new double[size];
        for (int i = 0; i < size; i++) {
            res[i] = buffer.getDouble();
        }
        return res;
    }
}
